'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	// certificatetype: "护照"
	// idnumber: "41027523213012312"
	// name: "123"
	// phone: "15037280828"
	// psgtype: "儿童"
	// type: "add"
	const { certificatetype, idnumber, name, phone, psgtype, type } = event
	let result = null
	try {
		const db = uniCloud.database()
		const PsgCollection = db.collection("passenger")
		//  查询
		if (type == "get") {
			result = await PsgCollection.get()
			if (result.data.length == 0) {
				return {
					code: -1,
					message: '查询失败'
				};
			} else {
				return {
					code: 0,
					message: '查询成功',
					result
				};
			}
			// 新增
		} else if (type == "add") {
			result = await PsgCollection.add(
				{
					"psgtype": psgtype,
					"name": name,
					"certificatetype": certificatetype,
					"idnumber": idnumber,
					"phone": phone
				}
			)

			return {
				code: 0,
				message: '新增成功'
			};

		}
	} catch (err) {
		return {
			code: -1,
			message: '查询失败'
		};
	}
};
